(function ($) {

  // Facebook post model
  window.FBPost = Backbone.Model.extend({
    defaults: {
      id: "",
      message: "",
      time: "",
      likes: 0,
      comments: 0,
      from_id: "",
      from_name: "",
      from_picture: "img/fb-default.gif",
      from_page: 0,
      pos_x: 0,
      pos_y: 0,
      type:""
    },
  });

  // Facebook post collection
  window.FBPosts = Backbone.Collection.extend({
    model: FBPost,

    // check if a fbPost is in the collection
    isIncluded: function(fbPost) {
      var posts = this.where({id: fbPost.id});
      if (posts.length>0)
        return true;
      else
        return false;
    },

    // return the older post
    getOlder: function() {
      var older = fbPosts.at(0);
      for (i=1; i<fbPosts.length; i++) {
        if (fbPosts.at(i).get('time') < older.get('time'))
          older = fbPosts.at(i);
      }
      return older;
    }
  });


  // Facebook post view
  window.FBPostView = Backbone.View.extend({
    tagName: "div",
    className: "fbpost-container",
    template: _.template($("#fbpost-template").html()),

    initialize: function() {
      this.model.on('change', this.render, this);
      this.model.on('destroy', this.remove, this);

      this.$el.id=this.model.get('id');
    },

    render: function() {
      this.$el.html(this.template(this.model.toJSON()));

      // changer la taille de l'image en fonction de la popularité (4 tailles dispos)
      nbLikes = this.model.get('likes');
      if (this.model.get('from_page')!=1) {
        if (nbLikes<5)
          this.$el.children()[1].className="img-size1";
        if (nbLikes>=5)
          this.$el.children()[1].className="img-size2";
        if (nbLikes>=10)
          this.$el.children()[1].className="img-size3";
        if (nbLikes>=20)
          this.$el.children()[1].className="img-size4";
      }
      else {
        if (nbLikes<20)
          this.$el.children()[1].className="img-size1";
        if (nbLikes>=200)
          this.$el.children()[1].className="img-size2";
        if (nbLikes>=2000)
          this.$el.children()[1].className="img-size3";
        if (nbLikes>=20000)
          this.$el.children()[1].className="img-size4";
      }
      return this;
    },

    remove: function() {
      this.$el.remove();
    },

    events: {
      "mouseenter" : "showMessage",
      "mouseleave" : "eraseMessage",
      "dblclick" : "openLink"
    },

    // message display
    showMessage: function() {
      this.$el.children()[0].className="visible"; // div id="fb-name"
      this.$el.children()[2].className="visible"; // div id="fb-bubble"
      /*// fb-template-try
      $(this.$el.children()[0]).children()[0].className="visible"; // div id="fb-name"
      $(this.$el.children()[0]).children()[2].className="visible"; // div id="fb-bubble"*/
      if (enableSound==1)
        this.tellMessage();
    },

    eraseMessage: function() {
      this.$el.children()[0].className="hidden";
      this.$el.children()[2].className="hidden";
    },

    // message spelling, using speak.js
    tellMessage: function() {
      speak(this.model.get('message'));
    },

    // open the link of the post in a new tab
    openLink: function() {
      window.open('https://www.facebook.com/'+this.model.get('id'));
    }
  });

  // Facebook collection view
  window.FBPostsCollectionView = Backbone.View.extend({
    el: $("#fbposts"),

    initialize: function() {
      this.render();

      this.collection.on("reset", this.render, this);
      this.collection.on("add", this.renderPost, this);
      this.collection.on("remove", this.removePost, this);
    },

    render: function() {
      this.$el.find("div").remove();

      _.each(this.collection.models, function (item) {
        this.renderPost(item);
      }, this);
    },

    renderPost: function(item) {
      var fbPostView = new FBPostView({
        model: item
      });
      this.$el.append(fbPostView.render().el);
    },

    removePost: function(item) {
      
    }
  });

  // Router
  window.FBPostsRouter = Backbone.Router.extend({
    routes: {
      "filter/:type": "urlFilter"
    },

    urlFilter: function (type) {
      fbPostsView.filterType = type;
      fbPostsView.trigger("change:filterType");
    }
  });

  window.fbPosts = new FBPosts();

  window.fbPostsView = new FBPostsCollectionView({
    collection: fbPosts
  });
  
  window.router = new FBPostsRouter();

  Backbone.history.start();

} (jQuery));
